/*
 * Copyright (C) Jochen Hiller, Bernd Kolb.
 * 
 * Licensed under Apache 2.0.
 * See http://www.apache.org/licenses/LICENSE-2.0
 */
package demo.location.tracker;

import android.app.Activity;
import android.database.Cursor;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TrackerActivity extends Activity {

	private LocationManager locationManager;
	private TextView output;
	private String bestLocationProvider;
	private MyLocationTracker myTracker = new MyLocationTracker();
	private DBRoute routeDB;
	private MyGeoDecoder geodecoder;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		output = (TextView) findViewById(R.id.output);

		routeDB = new DBRoute(this.getApplicationContext());
		geodecoder = new MyGeoDecoder(this.getApplicationContext());

		locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		bestLocationProvider = locationManager.getBestProvider(criteria, true);

		Button saveButton = (Button) findViewById(R.id.save_button);
		saveButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Log.e("LocationTracker", "save button clicked");
				Location loc = myTracker.getLastLocation();
				storeLocation(loc);
			}
		});

		Button deleteButton = (Button) findViewById(R.id.delete_button);
		deleteButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Log.e("LocationTracker", "delete button clicked");
				routeDB.deleteLocations();
				displayLocations();
			}
		});

		displayLocations();
	}

	@Override
	protected void onResume() {
		super.onResume();
		// Start updates (doc recommends delay >= 60000 ms)
		// 1 means notify when location change more than 1 meter
		locationManager.requestLocationUpdates(bestLocationProvider, 15000, 1,
				myTracker);
	}

	@Override
	protected void onPause() {
		super.onPause();
		// Stop updates to save power while app paused
		locationManager.removeUpdates(myTracker);
	}

	// private methods

	private void storeLocation(Location loc) {
		if (loc == null) {
			// display("<unknown>");
			// ignore
		} else {
			// display(loc.toString());
			// String desc = geodecoder.getNameForLocation(loc);
			String desc = "...";
			routeDB.addLocation(loc, desc);
			displayLocations();
		}
	}

	private void display(String string) {
		output.setText(string + "\n");
	}

	private void displayLocations() {
		Cursor cursor = routeDB.getLocations(TrackerActivity.this);
		String res = routeDB.showLocations(cursor);
		display(res);
	}

	private class MyLocationTracker implements LocationListener {

		private Location loc = null;

		public synchronized Location getLastLocation() {
			return loc;
		}

		public synchronized void onLocationChanged(Location location) {
			loc = location;
			storeLocation(loc);
		}

		public void onProviderDisabled(String provider) {
			// ignore for simplicity
		}

		public void onProviderEnabled(String provider) {
			// ignore for simplicity
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			// ignore for simplicity
		}

	}
}